package user;

import book.Book;
import book.Library;
import book.PariOfUidAndBookId;
import com.bit.utils.FileUtils;
import utils.AnalyzingBorrowBook;
import utils.Constant;

import javax.imageio.IIOException;
import java.util.Scanner;

public class NormalUser extends User {

    private PariOfUidAndBookId pariOfUidAndBookId[];
    private int  countBrrowe;
    private static final int BORROW_BOOK_MAX_NUM = 10;

    AnalyzingBorrowBook analyzingBorrowBook = new AnalyzingBorrowBook();
    Scanner scanner = new Scanner(System.in);

    private void loadBorrowedBook(){
        //1.读取信息
        PariOfUidAndBookId[] tem = analyzingBorrowBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
        //2.给数组赋值
        pariOfUidAndBookId = new PariOfUidAndBookId[BORROW_BOOK_MAX_NUM];
        //3.判断是否有读取信息
        if(tem == null) {
                System.out.println("文件内容为空，不可读取！");
                return;
        }

            //4.求长度
            int temLen = tem.length;
            //5.比较
            if (temLen > pariOfUidAndBookId.length) {
                //调整数组大小
                pariOfUidAndBookId = new PariOfUidAndBookId[temLen];
            }
            //6.赋值引用
            for (int i = 0; i < temLen; i++) {
                pariOfUidAndBookId[i] = tem[i];
            }
            //7.更新数组大小
            countBrrowe = temLen;

    }
  //写入
    private  void storeBorrowedBook(){
       analyzingBorrowBook.storeBook(pariOfUidAndBookId,Constant.BORROWED_BOOK_FILE_NAME);
    }

    Library library = new Library();
    public NormalUser(String name, int ID){
            super(name, ID);
            library = Library.getLibrary();
            loadBorrowedBook();

    }


    public int display(){
        System.out.println("普通用户 " + name + " 的操作菜单");
        System.out.println("1.查找图书");
        System.out.println("2.展示所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.借阅图书");
        System.out.println("5.归还图书");
        System.out.println("6.查看当前个人借阅情况");

        System.out.print("请输入你的操作：");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        return choice;
    }
    //搜索图书
    public void searchBook(){
        library.searchBook();
    }
    //展示图书
    public void displayBook(){
        library.displayBook();
    }
    //借阅图书
    public void borrowed(){
        if(library.getCountbook() == 0) {
            System.out.println("没有图书，不可借阅！");
            return;
        }
        int pos = library.searchBookByID();
        if(pos != -1){
            //遍历数组查找是否此书已经被借阅
            for (int i = 0; i < countBrrowe; i++) {
                //已被借阅
                if(pariOfUidAndBookId[i].getBookId() == library.getBooks(pos).getBookId()){
                    System.out.println("ID：" + pariOfUidAndBookId[i].getBookId() +
                            "的书籍已被借阅");
                    return;
                }
            }
        }
        library.borrowed(pos);

        //将借阅信息存储
        PariOfUidAndBookId p = new PariOfUidAndBookId(getID(),library.getBooks(pos).getBookId());
        pariOfUidAndBookId[countBrrowe] = p;

        //存储借阅书籍
        storeBorrowedBook();

    }
    //归还图书
    public void returnBook(){
        System.out.println("展示图书信息");
        library.displayBook();
        System.out.println("归还图书ID输入搜索");
        int pos = library.searchBookByID();
        if(pos != -1){
            if(getID() == pariOfUidAndBookId[pos].getUserId())
                library.returnBook(pos);
            else{
                System.out.println("ID:" + pariOfUidAndBookId[pos].getBookId()
                + "不是你借阅的书籍，无需归还！");
                return;
            }
        }
        for (int i = pos; i < pariOfUidAndBookId.length - 1; i++) {
            pariOfUidAndBookId[i] = pariOfUidAndBookId[i + 1];
        }
        pariOfUidAndBookId[countBrrowe - 1] = null;
        countBrrowe--;
        storeBorrowedBook();
    }
    //退出系统
    public void systemExit(){
        library.systemExit();
    }
    //查看当前个人借阅情况
    public void viewBorrowedBook(){
        loadBorrowedBook();
        System.out.println("***************************************");
        for (int i = 0; i < countBrrowe; i++) {
            if(getID() == pariOfUidAndBookId[i].getUserId()){
               library.viewBorrowedBook(i);
            }
        }
        System.out.println("***************************************");
        storeBorrowedBook();
    }
}

